<template>
  <div class="news-classify">
    <div style="display:flex;align-items: center;margin:0 0 10px;">
      <el-button size="small" type="primary" @click="add">新建系列</el-button>
    </div>
    <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
      <el-table-column label="ID" width="60">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="系列名称" width="180">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="封面图" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.pic" alt="" style="width:80px;height:80px;">
        </template>
      </el-table-column>
      <el-table-column label="系列简介" width="180">
        <template slot-scope="scope">
          {{ scope.row.acquireCond }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="获取条件" width="180">
        <template slot-scope="scope">
          {{ scope.row.acquireCond }}
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" width="180">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column label="证书状态" width="100">
        <template slot-scope="scope">
          <el-tag style="cursor:pointer;" size="small" v-if="scope.row.status == 0" type="danger">禁用</el-tag>
          <el-tag style="cursor:pointer;" size="small" v-else type="success">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="changeState(scope.row)" type="text" style="color:#ff0000;" v-if="scope.row.status == 1">禁用</el-button>
          <el-button @click="changeState(scope.row)" type="text" v-else>启用</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button @click="edit(scope.row)" type="text">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            @onConfirm="del(scope.row)"
            confirm-button-text='确定'
            cancel-button-text='取消'
            icon="el-icon-info"
            icon-color="red"
            title="是否删除本条数据？">
            <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin:10px 0 0 0;text-align:right;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10, 30, 50, 100]"
        :page-size="1"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageObj.total">
      </el-pagination>
    </div>

    <el-drawer
      title="系列设置"
      :visible.sync="drawerVisible"
      direction="rtl"
      size="60%"
      :before-close="handleClose">
      <div style="padding: 0 40px;height: 100%;position: relative;">
        <div style="display:flex;align-items:center;margin-bottom: 20px;">
          <div style="width: 100px;">系列名称：</div>
          <div style="flex: 1">
            <el-input clearable v-model="certObj.name" placeholder="请输入"></el-input>
          </div>
        </div>
        <div style="display:flex;margin-bottom: 20px;">
          <div style="width: 100px;">封面图：</div>
          <div style="flex: 1">
            <el-upload
              :class="imgFilesList.length?'hiddenUpload':''"
              action="https://service.bairicun.com/file/upload"
              list-type="picture-card"
              :on-success="handleSuccess"
              :on-preview="handlePictureCardPreview"
              :file-list="imgFilesList"
              :on-remove="handleRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </div>
        <div style="display:flex;margin-bottom: 20px;">
          <div style="width: 100px;">系列简介：</div>
          <div style="flex: 1">
            <el-input type="textarea" :rows="4" clearable v-model="certObj.intro" placeholder="请输入"></el-input>
          </div>
        </div>
        <div style="display:flex;margin-bottom: 20px;">
          <div style="width: 100px;">获取条件：</div>
          <div style="flex: 1">
            <el-input type="textarea" :rows="4" clearable v-model="certObj.acquireCond" placeholder="请输入"></el-input>
          </div>
        </div>
        <div style="display:flex;align-items:center;margin-bottom: 40px;">
          <div style="width: 100px;">证书状态：</div>
          <div style="flex: 1">
            <el-radio-group v-model="certObj.status">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </div>
        </div>
        <div style="display:flex;align-items:center;justify-content: flex-end;">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button v-if="certObj.id" @click="updateCert" type="primary">保 存</el-button>
          <el-button v-else @click="createCert" type="primary">添 加</el-button>
        </div>
        <div style="height:20px;"></div>
      </div>
    </el-drawer>

    <el-dialog :title="dailogTitle" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off" placeholder="请输入分类名称"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible">
      <img v-if="imgFilesList[0] && imgFilesList[0].url" width="100%" :src="imgFilesList[0].url" alt="">
    </el-dialog>
  </div>
</template>

<script>
import {
  getCertCategoryListApi,
  deleteCertCategoryApi,
  createCertCategoryApi,
  changeCertStatusApi,
  updateCertStatusApi
} from '@/api/certseries'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      dialogFormVisible: false,
      dailogTitle: '新建分类',
      form: {
        name: '',
        region: '',
      },
      formLabelWidth: '80px',
      currentPage: 1,
      pageObj: {
        pageSize: 10,
        pageNum: 1,
        total: 0,
      },
      saveId: '',
      drawerVisible: false,
      imgFilesList: [],
      dialogVisible: false,
      certObj: {
        id: '',
        name: '',
        intro: '',
        acquireCond: '',
        status: 1
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    closeDialog() {
      this.certObj.id = ''
      this.drawerVisible = false
    },

    edit(row) {
      // this.dailogTitle = '编辑分类'
      this.certObj.id = row.id
      this.certObj.status = row.status
      this.certObj.name = row.name
      this.imgFilesList = [
        {
          id: 19992233,
          url: row.pic
        }
      ]
      this.certObj.intro = row.intro
      this.certObj.acquireCond = row.acquireCond
      this.drawerVisible = true
      // getTypeDetailApi(row.id).then((res) => {
      //   this.dialogFormVisible = true
      //   this.form.name = res.data.name
      // })
    },
    createCert() {
      createCertCategoryApi({
        name: this.certObj.name,
        pic: this.imgFilesList[0].url,
        intro: this.certObj.intro,
        acquireCond: this.certObj.acquireCond,
        status: this.certObj.status
      }).then((res) => {
        this.fetchData()
        this.$message.success('操作成功！')
        this.closeDialog()
      })
    },
    updateCert() {
      updateCertStatusApi({
        id: this.certObj.id,
        name: this.certObj.name,
        pic: this.imgFilesList[0].url,
        intro: this.certObj.intro,
        acquireCond: this.certObj.acquireCond,
        status: this.certObj.status
      }).then((res) => {
        this.fetchData()
        this.$message.success('操作成功！')
        this.closeDialog()
      })
    },
    handleSuccess(e) {
      if (e.code == 200) {
        this.imgFilesList = [
          {
            url: e.data.url,
            id: e.data.id
          }
        ]
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleRemove() {
      this.imgFilesList = []
    },
    handleClose() {
      this.drawerVisible = false
    },
    onSubmit() {
      this.fetchData()
    },
    del(row) {
      deleteCertCategoryApi({
        id: row.id
      }).then((res) => {
        this.fetchData()
      })
    },
    add() {
      this.drawerVisible = true
      this.certObj.id = ''
    },
    fetchData() {
      this.listLoading = true
      getCertCategoryListApi({
        // title: this.form.title,
        // state: this.form.state,
        // pageSize: this.pageObj.pageSize,
        pageNum: this.pageObj.pageNum,
        pageSize: this.pageObj.pageSize
      }).then(response => {
        this.list = response.data.list
        this.pageObj.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageObj.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageObj.pageNum = val
      this.fetchData()
    },
    save() {
      saveTypeApi({
        id: this.saveId,
        name: this.form.name
      }).then((res) => {
        this.$message.success('操作成功！')
        this.cancel()
        this.fetchData()
      })
    },
    cancel() {
      this.saveId = ''
      this.dialogFormVisible = false
    },
    changeState(row) {
      changeCertStatusApi({
        id: row.id,
        status: row.status == 1? 0: 1,
      }).then((res) => {
        this.$message.success('操作成功！')
        this.fetchData()
      })
    },
    changeIsShow(row) {
      changeTypeStateApi({
        id: row.id,
        isShow: row.isShow == 1? 0: 1,
      }).then((res) => {
        this.$message.success('操作成功！');
        this.fetchData()
      })
    },
  }
}
</script>

<style>
  .el-drawer__body{
    overflow-x: hidden;
    overflow-y: auto;
  }
  .hiddenUpload .el-upload--picture-card{
    display:none !important;
  }
</style>

<style lang="scss" scoped>
   .news-classify{
     padding: 20px;
     .el-form-item{
       margin-bottom: 0;
     }
   }
</style>
